# -*- coding: utf-8 -*-
#
# Author:: Jonny
# Date:: 2018/12/5

import logging
import time
from logging import handlers

from config.common_conf import log_conf


class log:
    def __init__(self, **kwargs):
        # if "level" not in kwargs:
        #     logging.error("level 不存在")
        self.log_conf = kwargs
        self.level = kwargs["level"]
        self.format = kwargs["format"]
        self.datefmt = kwargs["datefmt"]
        # self.filename = kwargs["filename"]
        # self.filemode = kwargs["filemode"]
        self.handlers = kwargs["handlers"]

        log_conf_basic = {
            "level": self.level,
            "format": self.format,
            "datefmt": self.datefmt,
            # "filename": self.filename,
            # "filemode": self.filemode
        }

        handlers_d = []

        # 处理handlers
        if "StreamHandler" in self.handlers:
            if self.log_conf["handlers"]["StreamHandler"] == True:
                handler = logging.StreamHandler()
                handlers_d.append(handler)

        if "RotatingFileHandler" in self.handlers:
            handler = handlers.RotatingFileHandler(**self.log_conf["handlers"]["RotatingFileHandler"])
            handlers_d.append(handler)

        if "TimedRotatingFileHandler" in self.handlers:
            handler = handlers.TimedRotatingFileHandler(**self.log_conf["handlers"]["TimedRotatingFileHandler"])
            handlers_d.append(handler)

        logging.basicConfig(**log_conf_basic, handlers=handlers_d)


if __name__ == "__main__":
    log(**log_conf)
    for i in range(10):
        logging.info("哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈" + str(i))
